package accs.model.facebook;

import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import accs.model.AccountChecker;
import accs.model.Context;
import accs.model.ExtendedHttpClient;
import accs.model.utils.HeaderUtils;
import accs.model.utils.HtmlFormInputCollector;


public class FacebookChecker implements AccountChecker {

	public FacebookAccount login(String mail, String password, Context ctx) throws Exception {
		DefaultHttpClient httpclient=new ExtendedHttpClient(ctx);
		
		HttpGet get=new HttpGet("https://m.facebook.com/");
		get.setHeader(HeaderUtils.getUserAgent());
		HttpResponse resp=httpclient.execute(get);
		
		List<String> whitelist=new ArrayList<String>(Arrays.asList(new String[]{"lsd","charset_test","version","ajax","width","pxr","gps","m_ts","li","signup_layout","laststage","login","_fb_noscript"}));
		List<NameValuePair> list=HtmlFormInputCollector.collectInputs(new InputStreamReader(resp.getEntity().getContent(),"utf-8"),whitelist);
//		for(NameValuePair bla : list) {
//			System.out.println(bla.getName()+" -> "+bla.getValue());
//		}
		HttpPost post=new HttpPost("https://m.facebook.com/login.php?m=m&refsrc=http%3A%2F%2Fm.facebook.com%2F&refid=8");
		post.setHeader(HeaderUtils.getUserAgent());
		post.setHeader(HeaderUtils.getReferer("https://m.facebook.com/"));
		
		list.add(new BasicNameValuePair("email",mail));
		list.add(new BasicNameValuePair("pass",password));
		post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
		
		HttpResponse resp2=httpclient.execute(post);
		String location=resp2.getFirstHeader("Location").getValue();
		EntityUtils.consume(resp2.getEntity());
		
//		System.out.println("LOCATION "+location);
		
		//replace http by https
		location=location.replace("http://","https://");
		
		HttpGet insideGet=new HttpGet(location);
		insideGet.setHeader(HeaderUtils.getUserAgent());
		insideGet.setHeader(HeaderUtils.getReferer("https://m.facebook.com/"));
		HttpResponse resp3=httpclient.execute(insideGet);
		EntityUtils.consume(resp3.getEntity());
		//System.out.println(EntityUtils.toString(resp3.getEntity()));
		String userId=null;
		
		for(Cookie c : httpclient.getCookieStore().getCookies()) {
			System.out.println("COOKIE "+c.getName()+":"+c.getValue());
			if(c.getName().equals("c_user")) {
				userId=c.getValue();
			}
		}
		
		if(userId==null) {
			return null;
		} else {
			return new FacebookAccount(mail, password);
		}
	}

	public String getName() {
		return "fb";
	}

	public URL getImage() {
		return FacebookChecker.class.getResource("/accs/gui/img/facebook.png");
	}
}